package com.wonders.hisdoctor.station.project.vaccine.service.impl;

import com.wonders.his.common.utils.IpUtils;
import com.wonders.his.framework.dto.LoginUserDTO;
import com.wonders.his.framework.service.TokenService;
import com.wonders.his.framework.utils.oracle.IdUtils;
import com.wonders.his.framework.utils.oracle.OracleDateUtils;
import com.wonders.hisdoctor.station.enums.OracleSequenceEnum;
import com.wonders.hisdoctor.station.project.vaccine.domain.VaccineLog;
import com.wonders.hisdoctor.station.project.vaccine.mapper.VaccineLogMapper;
import com.wonders.hisdoctor.station.project.vaccine.service.IVaccineLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author zhanghaonan
 * @date 2021/6/25
 * @Description
 */
@Service
public class VaccineLogServiceImpl implements IVaccineLogService {

    private final static Logger logger = LoggerFactory.getLogger(VaccineLogServiceImpl.class);

    @Autowired
    private VaccineLogMapper vaccineLogMapper;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IdUtils idUtils;

    @Autowired
    private OracleDateUtils oracleDateUtils;

    @Override
    public int saveLog(Long operatorType, String content, HttpServletRequest request) {
        VaccineLog vaccineLog = new VaccineLog();
        //获取用户信息
        LoginUserDTO loginUserDTO = tokenService.getLoginUserDTO(tokenService.getToken(request));
        if (loginUserDTO != null && loginUserDTO.getSysSysuserVo() != null) {
            vaccineLog.setOperatorId(loginUserDTO.getSysSysuserVo().getEmpNo());
            vaccineLog.setOperatorName(loginUserDTO.getSysSysuserVo().getName());
            vaccineLog.setDeptCode(loginUserDTO.getSysSysuserVo().getDeptCode());
        }
        vaccineLog.setVaccineLogId(idUtils.getNextVal(OracleSequenceEnum.SEQ_ID_VACCINE_LOG.getCode()));
        String ip = IpUtils.getIpAddr(request);
        vaccineLog.setIpaddr(ip != null && ip.length() > 20 ? ip.substring(0, 20) : ip);
        logger.info("VACCINE_LOG_IP =======> " + ip);
        vaccineLog.setUpdateContent(content);
        vaccineLog.setOperatorType(operatorType);
        vaccineLog.setOperatorTime(oracleDateUtils.getOracleSysDate());
        return vaccineLogMapper.insert(vaccineLog);
    }
}
